package cn.ulc.socket.weapp.service.impl;

import cn.ulc.common.constant.PubDataConstant;
import cn.ulc.common.constant.enums.AvailableStatus;
import cn.ulc.socket.admin.common.BaseService;
import cn.ulc.socket.weapp.model.dto.IndexDTO;
import cn.ulc.socket.weapp.service.PageService;
import cn.ulc.store.dao.AdvertisingDao;
import cn.ulc.store.dao.NoticeDao;
import cn.ulc.store.dao.PubDataDao;
import cn.ulc.store.dao.site.SiteDao;
import cn.ulc.store.dao.template.TemplateBannerDao;
import cn.ulc.store.dao.template.TemplateBlockDao;
import cn.ulc.store.dao.template.TemplateBlockGoodsDao;
import cn.ulc.store.dao.template.TemplateCategoryDao;
import cn.ulc.store.entity.Advertising;
import cn.ulc.store.entity.Company;
import cn.ulc.store.entity.Notice;
import cn.ulc.store.entity.site.Site;
import cn.ulc.store.entity.template.TemplateBanner;
import cn.ulc.store.entity.template.TemplateBlock;
import cn.ulc.store.entity.template.TemplateBlockGoods;
import cn.ulc.store.entity.template.TemplateCategory;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class PageServiceImpl extends BaseService implements PageService {

    TemplateBannerDao bannerDao;
    TemplateBlockDao blockDao;
    TemplateBlockGoodsDao blockGoodsDao;
    NoticeDao noticeDao;
    PubDataDao pubDataDao;
    TemplateCategoryDao categoryDao;
    AdvertisingDao advertisingDao;
    SiteDao siteDao;
    @Override
    public IndexDTO loadIndexDTO() {
        Company company = getCurrentCompany();
        IndexDTO dto = new IndexDTO();
        String code = pubDataDao.findCodeByTypeAndName(PubDataConstant.DATA_TYPE_NOTICE_LOCATION,"首页公告");
        List<Notice> noticeList = noticeDao.findByCompany(company.getId(), code, AvailableStatus.AVAILABLE);
        dto.setNoticeList(noticeList);
        List<TemplateBanner> bannerList = bannerDao.findByCompanyAndStatus(company.getId(),AvailableStatus.AVAILABLE);
        dto.setBannerList(bannerList);
        List<TemplateBlock> blockList = blockDao.findByCompany(company.getId());
        List<TemplateBlockGoods> blockGoodsList = blockGoodsDao.findByCompany(company.getId());
        if(blockList != null && blockList.size()>0 && blockGoodsList != null && blockGoodsList.size()>0){
          blockList.stream().forEach(block->{
            Map<Boolean,List<TemplateBlockGoods>> map = blockGoodsList.stream().collect(Collectors.partitioningBy(t -> t.getTemplateBlockId().equals(block.getId())));
            block.setGoodsList(map.get(true));
          });
        }
        dto.setBlockList(blockList);
        List<TemplateCategory> categoryList = categoryDao.findByCompanyId(company.getId());
        dto.setCategoryList(categoryList);
        List<Advertising> advertisingList = advertisingDao.findList(company.getId(),AvailableStatus.AVAILABLE);
        Map<String,List<Advertising>> advertisingMap = advertisingList.stream().collect(Collectors.groupingBy(Advertising::getLocation));
        String frontCode = pubDataDao.findCodeByTypeAndName(PubDataConstant.DATA_TYPE_AD_LOCATION,"首页分类前");
        String behindCode = pubDataDao.findCodeByTypeAndName(PubDataConstant.DATA_TYPE_AD_LOCATION,"首页分类后");
        dto.setAdvertisingFrontList(advertisingMap.get(frontCode));
        dto.setAdvertisingBehindList(advertisingMap.get(behindCode));
        //所有分站
        Site site = siteDao.findDefault(company.getId());
        dto.setSite(site);
        return dto;
    }
}
